<template>
	<view class="page index">
		<view class="header">
			<common-search v-model="keyWord" @input="input" @filter="openPopup"></common-search>
		</view>
		<view class="main">
			<empty v-if="empty" :desc="desc"></empty>
			<scroll-view class="scroll" scroll-y="true" :show-scrollbar="false" :enhanced="true" @scrolltolower="scrollLower">
				<view class="project-list">
					<view class="project-item" v-for="item in agreementList" :key="item.id" @click="toDetails(item.id)">
						<view class="header">
							<view class="left">
								{{item.name}}
							</view>
							<view v-if="item.auditStatus == 30" class="right" :style="{color: `#bd3124`, borderColor: `#bd3124`}">
								不通过
							</view>
							<view v-else class="right" :style="{color: `${item.color}`, borderColor: `${item.color}`}">
								{{item.statusText}}
							</view>
						</view>
						<view class="pro-code">
							合同编号：{{item.no}}
						</view>
						<view class="company-money">
							<view class="company">
								签约客户：{{item.customerName}}
							</view>
							<view class="money">
								￥{{item.totalPrice}}
							</view>
						</view>
						<view class="details">
							<view class="det-item">
								<view class="tag">
									客户签约人
								</view>
								<view class="value">
									{{item.signContactName || '无'}}
								</view>
							</view>
							<view class="det-item">
								<view class="tag">
									签约日期
								</view>
								<view class="value">
									{{$share.getDate(item.orderDate)}}
								</view>
							</view>
							<view class="det-item">
								<view class="tag">
									回款率
								</view>
								<view class="value">
									{{item.totalPrice ? (item.totalReceivablePrice/item.totalPrice).toFixed(2) * 100 : 0}}%
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="create" @click="createAgreement">
				新建
			</view>
			<uni-popup ref="projectPopup" type="top" background-color="#fff">
				<view class="filter">
					<view class="filter-container status">
						<view class="title">
							合同状态
						</view>
						<view class="filter-list">
							<view :class="['item', statusIndex == index && 'select']" v-for="(item, index) in statusList" :key="index" @click="statusIndex = index">
								{{item}}
							</view>
						</view>
					</view>
					<view class="filter-container amount">
						<view class="title">
							合同金额
						</view>
						<view class="amoun-money">
							<span class="tag">￥</span>
							<input type="number" v-model="totalPriceRange[0]" placeholder="最低金额" placeholder-class="placeholder">
							<span class="text">至</span>
							<input type="number" v-model="totalPriceRange[1]" placeholder="最高金额" placeholder-class="placeholder">
						</view>
					</view>
					<view class="filter-container time">
						<view class="title">
							签订日期
						</view>
						<view class="select-time">
							<uni-datetime-picker v-model="timeRange" type="daterange" :end="Date.now()" rangeSeparator="至" @change="changeTime" />
						</view>
					</view>
					<view class="footer">
						<button class="cencle" @click="cencle">清空</button>
						<button class="confire" @click="confire">确定</button>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import commonSearch from "@/components/commonSearch.vue";
	import empty from "@/components/empty.vue"
	import {getAgreementListApi} from "@/api/agreement_api.js";
	export default {
		components: {
			commonSearch,
			empty
		},
		data() {
			return {
				empty: false,
				desc: '',
				keyWord: '',
				statusIndex: -1,
				statusList: ['待审核', '生效', '作废', '履行完毕'],
				totalPriceRange: [],
				timeRange:[],
				agreementList: [], //合同列表
				pageNum: 1,
				pageSize: 10,
				totalPage: 1
			}
		},
		onShow() {
			uni.showLoading({
				mask:true,
				title:'加载中...'
			})
			this.agreementList = [];
			this.pageNum = 1;
			this.getAgreementList();
		},
		methods: {
			//滚动线触底
			scrollLower(){
				if(this.pageNum >= this.totalPage){
					return;
				}
				uni.showLoading({
					mask:true,
					title:'加载中...'
				})
				this.pageNum++;
				this.getAgreementList();
			},
			//关键字
			input() {
				uni.showLoading({
					mask:true,
					title:'加载中...'
				})
				this.agreementList = [];
				this.pageNum = 1;
				this.getAgreementList();
			},
			// 获取合同列表
			async getAgreementList(){
				this.empty = false;
				if(this.keyWord == ''){
					this.desc = '暂无合同';
				}else{
					this.desc = '未搜索到合同'
				}
				let res = await getAgreementListApi({
					name: this.keyWord,
					status: this.statusIndex < 0 ? '' : this.statusIndex,
					totalPriceRange: this.totalPriceRange,
					orderDateRange: this.timeRange.length == 0 ? '' : `${this.timeRange[0]} 00:00:00, ${this.timeRange[1]} 23:00:00`,
					pageNo: this.pageNum,
					pageSize: this.pageSize
				}).catch(() => {
					uni.hideLoading();
					this.empty = true;
					return Promise.reject();
				})
				res.data.list = res.data.list.map((item) => {
					let statusText = "";
					let color = "";
					switch(item.status){
						case 0: {
							statusText = "审批中";
							color = "#f6d7ab";
							break;
						}
						case 1: {
							statusText = "生效";
							color = "#3388FF";
							break;
						}
						case 2: {
							statusText = "作废";
							color = "#F44444";
							break;
						}
						case 3: {
							statusText = "履行完毕";
							color = "#3BBF03";
							break;
						}
						default: {
							statusText = "";
							color = ""
							break;
						}
					}
					return {
						...item,
						statusText,
						color
					}
				})
				this.agreementList = [...this.agreementList, ...res.data.list];
				if(res.data.total == 0){
					this.empty = true;
				}
				this.totalPage = Math.ceil(res.data.total / this.pageSize);
				uni.hideLoading();
			},
			// 清空筛选
			cencle(){
				this.statusIndex = -1;
				this.teamIndex = -1;
				this.totalPriceRange = [];
				this.timeRange = [];
			},
			confire(){
				this.$refs.projectPopup.close();
				uni.showLoading({
					mask:true,
					title:'加载中...'
				})
				this.agreementList = [];
				this.pageNum = 1;
				this.getAgreementList();
			},
			changeTime(){
			},
			openPopup(){
				this.$refs.projectPopup.open();
			},
			toDetails(id) {
				uni.navigateTo({
					url: `/pages/index/agreement/details?id=${id}`
				})
			},
			createAgreement() {
				uni.navigateTo({
					url: `/pages/index/agreement/create?type=contract`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/common/style/customicons.scss";

	.index {
		@include flex-c;

		>.header {
			padding: 24rpx 30rpx 0;
			flex: 0 0 auto;
		}

		.main {
			flex: 1 0 0;
			overflow: hidden;

			.scroll{
				height: 100%;
				.project-list {
					padding: 24rpx 30rpx;
				
					.project-item {
						@include box;
						@include list;
						margin: 0 0 18rpx;
						.pro-code{
							color: #9A9A9A;
							font-size: 24rpx;
						}
						.company-money{
							@include flex-r-between-center;
							.company{
								color: #9A9A9A;
								font-size: 24rpx;
							}
							.money{
								color: #FCCA00;
								font-size: 26rpx;
							}
						}
					}
				}
			}

			.create {
				position: fixed;
				bottom: 106rpx;
				right: 30rpx;
				width: 80rpx;
				height: 80rpx;
				background-color: #4095E5;
				@include flex-r-center-center;
				font-size: 24rpx;
				color: #fff;
				border-radius: 50%;
				box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
			}
			.filter {
				@include popup;
				.amount{
					.amoun-money{
						@include flex-r-between-center;
						border: 4rpx #E0E3EA solid;
						border-radius: 6rpx;
						height: 88rpx;
						padding: 0 20rpx;
						.tag{
							color: #C1C2C5;
							font-size: 38rpx;
						}
						.text{
							color: #101010;
							font-size: 32rpx;
						}
						input{
							text-align: center;
							font-size: 32rpx;
							color: #C4C7CF;
							padding: 0 10rpx;
						}
						.placeholder{
							text-align: center;
							color: #C4C7CF;
							font-size: 32rpx;
						}
					}
				}
				.time{
					.select-time{
						padding: 20rpx 0 0;
					}
				}
			}

			&::-webkit-scrollbar {
				display: none;
			}
		}
	}
	::v-deep .uni-calendar--fixed {
		top: 0;
		bottom: unset;
	}
</style>